/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Temporal;
import javax.persistence.Transient;
import org.apache.log4j.Logger;

/**
 *
 * @author wei
 */
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class DataObjectEntity implements Serializable {

    private static final long serialVersionUID = 1000L;
    @Transient
    protected Logger log = Logger.getLogger(getClass());
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private PersonEntity createdBy;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date createdAt;
    private PersonEntity lastModifiedBy;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date lastModifiedAt;
    private boolean archived;

    public DataObjectEntity() {
        createdAt = new Date();
        lastModifiedAt = new Date();
        archived = false;
        createdBy = null;
        lastModifiedBy = null;
    }

    public DataObjectEntity(PersonEntity createdBy) {
        this();
        this.createdBy = createdBy;
        this.lastModifiedBy = createdBy;
    }

    public void modified(PersonEntity modifiedBy) {
        this.lastModifiedBy = modifiedBy;
        this.lastModifiedAt = new Date();
        if (modifiedBy == null) {
            log.info(getClass() + " Object modified by self");
            log.info(childToString());
            log.info(superToString());
        } else {
            log.info(getClass() + " Object modified by " + modifiedBy.getName());
            log.info(childToString());
            log.info(superToString());
        }
    }

    public void created(PersonEntity createdBy) {
        this.createdBy = createdBy;
        this.lastModifiedBy = createdBy;
        if (createdBy == null) {
            log.info(getClass() + " Object " + id + " created by self");
            log.info(childToString());
            log.info(superToString());
        } else {
            log.info(getClass() + " Object " + id + " created by " + createdBy.getName());
            log.info(childToString());
            log.info(superToString());
        }
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public boolean isArchived() {
        return archived;
    }

    public void setArchived(boolean archived) {
        this.archived = archived;
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public PersonEntity getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(PersonEntity createdBy) {
        this.createdBy = createdBy;
    }

    public Date getLastModifiedAt() {
        return lastModifiedAt;
    }

    public void setLastModifiedAt(Date lastModifiedAt) {
        this.lastModifiedAt = lastModifiedAt;
    }

    public PersonEntity getLastModifiedBy() {
        return lastModifiedBy;
    }

    public void setLastModifiedBy(PersonEntity lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof DataObjectEntity)) {
            return false;
        }
        DataObjectEntity other = (DataObjectEntity) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entity.DataObjectEntity[ id=" + id + " ]";
    }

    public String superToString() {
        return "DataObjectEntity{" + "createdBy=" + createdBy + ", createdAt=" + createdAt + ", lastModifiedBy=" + lastModifiedBy + ", lastModifiedAt=" + lastModifiedAt + ", archived=" + archived + '}';
    }

    public abstract String childToString();
}
